<template>
  <div class="main">
    <div class="login-container">
      <div class="login-card">
        <div class="login-left">
          <div class="pic"></div>
        </div>
        <div class="login-right">
          <div class="form">
            <h1 class="title">宙斯健身馆</h1>
            <el-form
                class="login-form"
                :model="loginFormModel"
                :rules="rules"
                ref="loginFormRef"
                @keyup.enter="submitLogin"
            >
              <el-form-item prop="username">
                <el-input
                    v-model="loginFormModel.username"
                    placeholder="请输入用户名"
                    prefix-icon="User"
                    size="large"
                    clearable
                    autocomplete="username"
                />
              </el-form-item>

              <el-form-item prop="password">
                <el-input
                    type="password"
                    v-model="loginFormModel.password"
                    placeholder="请输入密码"
                    prefix-icon="Lock"
                    show-password
                    size="large"
                    autocomplete="current-password"
                />
              </el-form-item>

              <el-form-item prop="captcha">
                <div class="captcha-wrapper">
                  <el-input
                      placeholder="请输入验证码"
                      v-model="loginFormModel.captcha"
                      maxlength="4"
                      size="large"
                      autocomplete="off"
                  />
                  <img
                      class="captcha-img"
                      :src="captchaUrl"
                      @click="refreshCaptcha"
                      alt="验证码"
                      title="点击刷新验证码"
                  >
                </div>
              </el-form-item>

              <el-form-item>
                <el-button
                    type="primary"
                    class="login-btn"
                    @click="submitLogin"
                    :loading="loading"
                    size="large"
                    round
                    block
                >
                  {{ loading ? '登录中...' : '登 录' }}
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>

    <div class="copyright">
      <p>&copy; 2025 宙斯健身馆 版权所有</p>
    </div>
  </div>
</template>

<style scoped>
.main {
  height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
  url("@/assets/loginBackground.png") no-repeat center center/cover;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  flex: 1;
}

.login-card {
  display: flex;
  width: 900px;
  height: 500px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  backdrop-filter: blur(10px);
}

.login-left {
  flex: 1;
  background: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)),
  url("@/assets/loginBody.jpg") no-repeat center center/cover;
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #1e3c72;
  font-size: 28px;
  font-weight: bold;
}

.login-form {
  width: 100%;
}

.captcha-wrapper {
  display: flex;
  gap: 10px;
}

.captcha-wrapper .el-input {
  flex: 1;
}

.captcha-img {
  height: 40px;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.3s;
  border: 1px solid #dcdfe6;
}

.captcha-img:hover {
  transform: scale(1.05);
}

.login-btn {
  margin-top: 20px;
  font-weight: bold;
  letter-spacing: 2px;
}

.copyright {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
}

.copyright p {
  margin: 0;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    width: 90%;
    flex-direction: column;
    height: auto;
  }

  .login-left {
    height: 200px;
  }

  .login-right {
    padding: 20px;
  }
}
</style>

<script setup>
import {ref, reactive, onMounted, toRaw} from "vue"
import {ElMessage} from "element-plus"
import login from "@/api/login.js"
import { saveJwt } from "@/api/jwt.js"
import router from "@/router/index.js";
import {v4 as uuid} from "uuid"

const loading = ref(false)
const captchaId = ref();
const captchaUrl = ref();
// 表单引用
const loginFormRef = ref()

// 登录表单数据
const loginFormModel = reactive({
  username: "",
  password: "",
  captcha: ""
})

// 表单验证规则
const rules = {
  username: [
    {required: true, message: "请输入用户名", trigger: "blur"}
  ],
  password: [
    {required: true, message: "请输入密码", trigger: "blur"},
    {min: 6, message: "密码长度不能少于6位", trigger: "blur"}
  ],
  captcha: [
    {required: true, message: "请输入验证码", trigger: "blur"},
    {len: 4, message: "验证码必须为4位字符", trigger: "blur"}
  ]
}

// 刷新验证码
function refreshCaptcha() {
  let id=uuid();
  captchaId.value=id;
  captchaUrl.value = `/api/login/captcha?id=`+id;
}

// 提交登录
async function submitLogin() {
  if (loading.value) return

  try {
    await loginFormRef.value.validate()

    loading.value = true

    const formData = {
      username: loginFormModel.username.trim(),
      password: loginFormModel.password,
      captcha: loginFormModel.captcha.trim(),
      captchaId: captchaId.value
    }
    let mode=toRaw(loginFormModel);
    mode.captchaId=captchaId.value;
    const resp = await login(formData)

    console.log("登录结果:", formData, resp)
    if (resp.success) {
      saveJwt(resp.data)
      ElMessage.success("登录成功")
      await router.push("/main/chart")
    } else {
      ElMessage.error(resp.message || "登录失败")
      refreshCaptcha()
      loginFormModel.captcha = ""
    }
  } catch (error) {
    console.error("登录异常:", error)
    ElMessage.error("登录过程中发生错误，请稍后重试")
    refreshCaptcha()
  } finally {
    loading.value = false
  }
}

// 组件挂载时刷新验证码
onMounted(() => {
  refreshCaptcha()
})
</script>
